import json

from flask import *
from process import *

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/scripts/<path:filename>', methods=['GET'])
def scripts(filename):
    return send_from_directory('scripts', filename)

@app.route('/css/<path:filename>', methods=['GET'])
def css(filename):
    return send_from_directory('css', filename)

@app.route('/data/<path:filename>', methods=['GET'])
def data(filename):
    if(filename.endswith('.json')):
        with open("./data/" + filename, "r", encoding='utf-8') as file:
            return jsonify(json.load(file))
    return send_from_directory('data', filename)

processor = Processor()
@app.route('/postData', methods=['POST'])
def postData():
    processor.updateData(request.get_json())
    return jsonify({"status": "success"})

@app.route('/getEmbedNodeData', methods=['Get'])
def getEmbedNodeData():  
    result = processor.getEmbedNodeData()
    # print(result)
    return jsonify(result)

@app.route('/getEmbedEdgeData', methods=['Get'])
def getEmbedEdgeData():  
    result = processor.getEmbedEdgeData()
    # print(result)
    return jsonify(result)

@app.route('/getCenterRelation', methods=['Get'])
def getCenterRelation():  
    result = processor.getCenterRelation()
    # print(result)
    return jsonify(result)

@app.route('/postFilterCondition', methods=['Post'])
def postFilterConditionn():  
    processor.updateFilterCondition(request.get_json())
    return jsonify({"status": "success"})

@app.route('/getFilteredData', methods=['Get'])
def getFilteredData():  
    result = processor.getFilterData(processor.condition)
    # print(result)
    return jsonify(result)

if __name__ == '__main__':
    app.run(port=8080)
